﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace project
{
    public partial class CustomerUpdate : Form
    {
        public CustomerUpdate()
        {
            InitializeComponent();
        }

        private void buttonCLose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void InitData()
        {
            //初始化数据
            string sql = "Select * from Customer";
            DataTable dt = DBHelper.GetTable(sql);
            this.dataGridView1.DataSource = dt;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }
        private void CustomerUpdate_Load(object sender, EventArgs e)
        {
            InitData();
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            string id= this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            string sql = "SELECT * FROM [dbo].[Customer] where CustomerNo="+id;
            DataTable dt= DBHelper.GetTable(sql);
            foreach (DataRow item in dt.Rows)
            {
                this.textBoxId.Text = item["CustomerNo"].ToString();
                this.textBoxName.Text = item["CustomerName"].ToString();
                this.textBoxAddress.Text = item["Address"].ToString();
                this.textBoxPhone.Text = item["Phone"].ToString();
                this.textBoxEmployer.Text = item["Employer"].ToString();
                string sex= item["CustomerNo"].ToString();
                if (sex.Contains("男"))
                {
                    this.radioButton1.Checked = true;
                    return;
                }this.radioButton2.Checked = true;
            }
        }

        private void buttonClose_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            string id = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            string sql = "delete from Customer where CustomerNo=" + id;
            int re= DBHelper.ExecuteNonQuery(sql);
            if (re>0)
            {
                InitData();return;
            }
            MessageBox.Show("删除失败，请重试");
        }

        private void buttonUpdatae_Click(object sender, EventArgs e)
        {
            string address = this.textBoxAddress.Text, name = this.textBoxName.Text, phone = this.textBoxPhone.Text, employer = this.textBoxEmployer.Text, sex = "女", Id = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            
            if (this.radioButton1.Checked)
            {
                sex = "男";
            }
            string sql = "UPDATE [dbo].[Customer]SET [CustomerName] = '{0}',[Address] ='{1}' ,[Phone] ='{2}' ,[Employer] ='{3}' ,[Sex] ='{4}' WHERE CustomerNo={5}";
            sql = string.Format(sql,name,address,phone,employer,sex,Id);
            int re= DBHelper.ExecuteNonQuery(sql);
            if (re>0)
            {
                MessageBox.Show("修改成功");
                InitData();
                return;
            }MessageBox.Show("修改失败");
            
            
        }
    }
}
